﻿#include "mytabwidget.h"

MyTabwidget::MyTabwidget(QWidget *parent) :
    QTabWidget(parent)
{
    tabBar()->installEventFilter(this);
}

bool MyTabwidget::eventFilter(QObject *o, QEvent *e)
{
    // double click to close a tab
    if(o == tabBar() && e->type() == QEvent::MouseButtonDblClick) {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
        removeTab(tabBar()->tabAt(mouseEvent->pos()));
        return true;
    }
    // click the last tab
    if(o == tabBar() && e->type() == QEvent::MouseButtonRelease) {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
        if (tabBar()->tabAt(mouseEvent->pos())
                == tabBar()->count() - 1)
        emit(lastTabClicked());
        return true;
    }

    return QTabWidget::eventFilter(o, e);
}
